package com.umer.pinban.user;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.recycleview.PullLoadMoreRecyclerView;
import com.common.ui.recycleview.listener.DataStateListener;
import com.common.ui.recycleview.listener.OptListener;
import com.common.ui.statelayout.StateLayout;
import com.common.utils.AntiShake;
import com.common.utils.Constants;
import com.common.utils.ItemClickSupport;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.bean.user.SelectMemberRecordListBean;
import com.umer.pinban.user.adapter.SelectMemberRecordListAdapter;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

public class XuFeiRecordListActivity extends BaseActivity implements DataStateListener,
        PullLoadMoreRecyclerView.PullLoadMoreListener, OptListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.pullLoadMoreRecyclerView)
    PullLoadMoreRecyclerView pullLoadMoreRecyclerView;
    @BindView(R.id.state_layout)
    StateLayout stateLayout;
    private SelectMemberRecordListAdapter selectMemberRecordListAdapter;
    private List<SelectMemberRecordListBean> infos = new ArrayList<>();
    private UserInfo userInfo;

    @Override
    public View isSupportLoadSirEnabled() {
        return null;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_xufei_record_list;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        initView();
    }

    @Override
    public void loadData() {
        if (selectMemberRecordListAdapter.getPage() == 1) {
            stateLayout.showLoadingView();
        }
        selectMemberRecordList();
    }

    private void selectMemberRecordList(){
        OkEntityListRequest<SelectMemberRecordListBean> request = new OkEntityListRequest<>(R.id.selectMemberRecordList,
                Constants.selectMemberRecordList, SelectMemberRecordListBean.class);
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("page", selectMemberRecordListAdapter.getPage());
        request.addParams("pageSize", 10);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    @OnClick({R.id.linear_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                if (AntiShake.check(view.getId())) {
                    return;
                }
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {

    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.selectMemberRecordList:
                if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
                    swipeRefreshLayout.setRefreshing(false);
                }
                infos = (List<SelectMemberRecordListBean>) t.getT();
                selectMemberRecordListAdapter.addDataSource(infos, t);
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.selectMemberRecordList:
                if (swipeRefreshLayout != null && swipeRefreshLayout.isRefreshing()) {
                    swipeRefreshLayout.setRefreshing(false);
                }
                infos = (List<SelectMemberRecordListBean>) t.getT();
                selectMemberRecordListAdapter.addDataSource(infos, t);
                break;
            default:
                break;
        }
    }

    @Override
    public void onRefresh() {
        selectMemberRecordListAdapter.reset();
        selectMemberRecordList();
    }

    @Override
    public void onLoadMore() {
        loadData();
    }

    @Override
    public void onDataState(int flag) {
        //1:没有网络 2:没有数据 3:获取数据失败 4:显示内容
        switch (flag) {
            case 1:
                stateLayout.showNoNetworkView();
                break;
            case 2:
                stateLayout.showEmptyView();
                break;
            case 3:
                stateLayout.showErrorView();
                break;
            case 4:
                stateLayout.showContentView();
                break;
            default:
                break;
        }
    }

    private void initView() {

        stateLayout.setRefreshListener(new StateLayout.OnViewRefreshListener() {
            @Override
            public void refreshClick() {
                loadData();
            }

            @Override
            public void loginClick() {
            }
        });
        pullLoadMoreRecyclerView.init(recyclerView, swipeRefreshLayout);
        selectMemberRecordListAdapter = new SelectMemberRecordListAdapter(this,
                R.layout.activity_xu_fei_list_item, pullLoadMoreRecyclerView,
                this, this);
        pullLoadMoreRecyclerView.setAdapterTwo(selectMemberRecordListAdapter);
        pullLoadMoreRecyclerView.setOnPullLoadMoreListener(this);
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        swipeRefreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.colorAccent, R.color.colorAccent, R.color.colorAccent);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法

        //单击
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView())
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                    }
                });
        //长按
        ItemClickSupport.addTo(pullLoadMoreRecyclerView.getRecyclerView()).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
                return false;
            }
        });
    }
}
